#!/usr/sbin/rsct/perl5/bin/perl
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
#  
#  
# Licensed Materials - Property of IBM 
#  
# (C) COPYRIGHT International Business Machines Corp. 2003,2004 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 
#"@(#)27   1.2   src/rsct/rm/SensorRM/cli/bin/refsensor.perl, sensorcli, rsct_rzauh, rzauh0431a 10/21/03 00:37:13"

# Refreshes the a sensor resource in the RMC IBM.Sensor class

use strict;
use locale;
BEGIN
  {
 # this enables us to redirect where it looks for other RSCT files during development
 $::rsctroot = $ENV{'RSCT_ROOT'} || '/usr/sbin/rsct';
 $::rsctpm = "$::rsctroot/pm";
 $::rsctmsgmaps = "$::rsctroot/msgmaps";
  }

use lib $::rsctpm;
use Getopt::Std;
use CT_cli_utils qw(printIMsg printEMsg);
use Socket;

$main::PROGNAME = 'refsensor';
$main::MSGCAT = 'sensorcli.cat';
$main::LSMSG = '/usr/sbin/rsct/bin/ctdspmsg';

# For the usage, see sensorcli.msg
sub usage { printIMsg('IMsgRefsensorUsage');  exit (scalar(@_) ? $_[0] : 1); }

# Parse the cmd line args and check them
if (! getopts('an:hvV') ) { &usage; }
if (scalar(@ARGV) < 1 || $::opt_h) { &usage; }
if ($::opt_a && $::opt_n) { &usage; }
if ($::opt_V) { $::opt_v = 1; }

my $name = shift @ARGV;

if ($::opt_a || defined($::opt_n))
  {
 #todo: support symbolic names for values 2 and 3
 if ($ENV{CT_MANAGEMENT_SCOPE}!=2 && $ENV{CT_MANAGEMENT_SCOPE}!=3) { $ENV{CT_MANAGEMENT_SCOPE}=4; }
  }
else { $ENV{CT_MANAGEMENT_SCOPE}=1; }

my $nodename="";
if (defined($::opt_n))
  {
     $nodename = $::opt_n;
  }

# fill in FieldIndicator from the args passed
my $fld_ind=0;
foreach my $b (@ARGV)
  {
     # Note: we allow a null value
     my ($at, $val) = $b =~ /^\s*(\S+?)\s*=\s*(\S*.*)$/;

     if    ($b =~ /^String=/)  { 
           $fld_ind += 1; 

        if (!length($val)) { $val = "''"; }     # chrsrc-api needs something for the value
        # Put quotes around the value to protect spaces or weird chars
        # (if the value already has quotes around it, do not quote it)
        if (!( $val =~ /^(".*"|'.*')$/ ))
          {
             # if the value has imbedded double quotes, use single quotes
             if ($val =~ /\"/) { $val = qq('$a'); }   # use the other quote
             else   # use double quotes
               {
                 $val =~ s/\"/\\\"/sg;     # escape embedded double quotes
                 $val = qq("$val");
                }
          }
     }

     elsif ($b =~ /^Int32=/)   { $fld_ind += 2; }
     elsif ($b =~ /^Uint32=/)  { $fld_ind += 4; }
     elsif ($b =~ /^Int64=/)   { $fld_ind += 8; }
     elsif ($b =~ /^Uint64=/)  { $fld_ind += 16; }
     elsif ($b =~ /^Float32=/) { $fld_ind += 32; }
     elsif ($b =~ /^Float64=/) { $fld_ind += 64; }
     elsif ($b =~ /^Quantum=/) { $fld_ind += 128; }
     else { 
           printEMsg('EMsgRefsensorInvAttr',$b);
           exit(1);
          }

     $b = "${at}::$val";

  }

my $attrvals = join('::', @ARGV);

my $cmd = "";

# decide which runact to do ... no args to RunCommand, else SetValues.
if (scalar(@ARGV) < 1 )
   {
      # no args so use RunCommand action
      $cmd = qq(/usr/bin/runact-api -o IBM.Sensor::${name}::${nodename}::RunCommand::WaitCommandEnd::1 2>&1);
   }
else
   {
      # args so use SetValues
      $cmd = qq(/usr/bin/runact-api -o IBM.Sensor::${name}::${nodename}::SetValues::FieldIndicator::${fld_ind}::${attrvals} 2>&1);

   }


if ($::opt_v) { $main::PROGNAME = 'lssensor'; printIMsg('IMsgRmcCmd', $cmd); $main::PROGNAME = 'refsensor'; }

my @output = `$cmd`;
my $rc = $? >> 8;

# if rc not 0, then some messages may be mixed in with action output
my @no_error_output =();
if ($rc)
  {
     foreach my $line (@output)
        {
             # is it an error message? if so print, if not, save
             if ($line =~ /^ERROR/) 
                { 
                   if (!$::opt_v) { $line =~ s/.*::.*::.*::.*::.*:://; }
                   print $line;
                } 
             else
                {
                   push (@no_error_output,$line);
                } 
        }
  }

else {@no_error_output = @output;}

# process action output 
# from RunCommand: Result=  0=success, 1=sensor not monitored, 2=error
# from SetValues:  Result=  Same as FieldIndicator  (Note: if Result=0, nothing updated)
#                           256=sensor not monitored

# get action Result value
(my $handle, my $result) = split /::Result::/, $no_error_output[0];

# if SetValue  
if ($fld_ind) 
   {
      if ($result != $fld_ind)
         {
 
           $rc = 4;
           if ( ($fld_ind & 1) && !($result & 1) ) {
                 printEMsg('EMsgRefsensorNoUpdate',"String");
              }
           if ( ($fld_ind & 2) && !($result & 2) ) {
                 printEMsg('EMsgRefsensorNoUpdate',"Int32");
              }
           if ( ($fld_ind & 4) && !($result & 4) ) {
                 printEMsg('EMsgRefsensorNoUpdate',"Uint32");
              }
           if ( ($fld_ind & 8) && !($result & 8) ) {
                 printEMsg('EMsgRefsensorNoUpdate',"Int64");
              }
           if ( ($fld_ind & 16) && !($result & 16) ) {
                 printEMsg('EMsgRefsensorNoUpdate',"Uint64");
              }
           if ( ($fld_ind & 32) && !($result & 32) ) {
                 printEMsg('EMsgRefsensorNoUpdate',"Float32");
              }
           if ( ($fld_ind & 64) && !($result & 64) ) {
                 printEMsg('EMsgRefsensorNoUpdate',"Float64");
              }
           if ( ($fld_ind & 128) && !($result & 128) ) {
                 printEMsg('EMsgRefsensorNoUpdate',"Quantum");
              }
                 
         }
   }

# else RunCommand
else
   {
      if ($result == 1)  
         {
            printEMsg('EMsgRefsensorNotMon');
            $rc = 4;
         }
   }


exit $rc;

